cmake_minimum_required(VERSION 3.16)

project(BacteriaSamplerSystem VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Find required Qt components
find_package(Qt6 REQUIRED COMPONENTS Core Widgets Qml Quick QuickControls2 Charts Sql)

# Enable automatic MOC, UIC, and RCC processing
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

# Source files
set(SOURCES
    main.cpp
    devicecontroller.cpp
    samplingmanager.cpp
    qmlbridge.cpp
    datavisualization.cpp
    auditlogger.cpp
    usersession.cpp
    permissionmanager.cpp
    databasehandler.cpp
    mainwindow.cpp
)

# Header files
set(HEADERS
    devicecontroller.h
    samplingmanager.h
    qmlbridge.h
    datavisualization.h
    auditlogger.h
    usersession.h
    permissionmanager.h
    databasehandler.h
    mainwindow.h
)

# QML and resource files
set(QML_SOURCES
    qml.qrc
)

# Create executable
qt6_add_executable(BacteriaSamplerSystem
    ${SOURCES}
    ${HEADERS}
    ${QML_SOURCES}
)

# Link Qt libraries
target_link_libraries(BacteriaSamplerSystem
    Qt6::Core
    Qt6::Widgets
    Qt6::Qml
    Qt6::Quick
    Qt6::QuickControls2
    Qt6::Charts
    Qt6::Sql
)

# Set target properties for embedded system
set_target_properties(BacteriaSamplerSystem PROPERTIES
    WIN32_EXECUTABLE TRUE
    MACOSX_BUNDLE TRUE
)

# Compiler-specific options
if(MSVC)
    target_compile_options(BacteriaSamplerSystem PRIVATE /W4)
else()
    target_compile_options(BacteriaSamplerSystem PRIVATE -Wall -Wextra -Wpedantic)
endif()

# Install rules
install(TARGETS BacteriaSamplerSystem
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

# Copy Qt runtime libraries for Windows deployment
if(WIN32)
    find_program(WINDEPLOYQT_EXECUTABLE windeployqt HINTS ${Qt6_DIR}/../../../bin)
    if(WINDEPLOYQT_EXECUTABLE)
        add_custom_command(TARGET BacteriaSamplerSystem POST_BUILD
            COMMAND ${WINDEPLOYQT_EXECUTABLE} --qmldir ${CMAKE_SOURCE_DIR} $<TARGET_FILE:BacteriaSamplerSystem>
            COMMENT "Deploying Qt libraries")
    endif()
endif()

# Set application icon and metadata
if(WIN32)
    set_target_properties(BacteriaSamplerSystem PROPERTIES
        WIN32_EXECUTABLE TRUE
    )
endif()

# Debug configuration
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    target_compile_definitions(BacteriaSamplerSystem PRIVATE DEBUG_BUILD)
endif()

# Embedded system specific settings
if(DEFINED EMBEDDED_TARGET)
    target_compile_definitions(BacteriaSamplerSystem PRIVATE EMBEDDED_SYSTEM)
    # Add embedded-specific optimizations
    target_compile_options(BacteriaSamplerSystem PRIVATE -Os)
endif()